package com.whfc.base.redis.impl;

import com.whfc.base.dto.AppWarnFreqConfigDTO;
import com.whfc.base.redis.WarnConfigRedisDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.text.MessageFormat;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author sun_guodong
 * @description
 * @date 2020-12-18
 */
@Repository
public class WarnConfigRedisDaoImpl implements WarnConfigRedisDao {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RedisTemplate<String, AppWarnFreqConfigDTO> redisTemplate;

    private static final String WARN_CONFIG_KEY = "warn-config::{0}::{1}::{2}::{3}";

    private static final int EXPIRE_HOURS = 24;

    @Override
    public void setWarnConfig(Integer deptId, Integer warnModuleType, Integer type, Integer ruleType, AppWarnFreqConfigDTO value) {
        logger.info("设置报警设置的缓存,deptId:{},warnModuleType:{},type:{},ruleType:{},value:{}", deptId, warnModuleType, type, ruleType, value.toString());
        String key = MessageFormat.format(WARN_CONFIG_KEY, deptId, warnModuleType, type, ruleType);
        redisTemplate.opsForValue().set(key, value, EXPIRE_HOURS, TimeUnit.HOURS);
    }

    @Override
    public AppWarnFreqConfigDTO getWarnConfig(Integer deptId, Integer warnModuleType, Integer type, Integer ruleType) {
        logger.info("获取报警设置的缓存,deptId:{},warnModuleType:{},type:{},ruleType:{}", deptId, warnModuleType, type, ruleType);
        String key = MessageFormat.format(WARN_CONFIG_KEY, deptId, warnModuleType, type, ruleType);
        AppWarnFreqConfigDTO appWarnFreqConfigDTO = redisTemplate.opsForValue().get(key);
        return appWarnFreqConfigDTO;
    }

    @Override
    public void delWarnConfig() {
        Set<String> keys = redisTemplate.keys("warn-config:*");
        redisTemplate.delete(keys);
    }
}
